package com.lala.rennigou.service;

import com.lala.rennigou.mapper.EasybuyUserAddressMapper;
import com.lala.rennigou.pojo.EasybuyUserAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.TransactionAnnotationParser;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

@Service
public class EasybuyUserAddressServiceImpl implements EasybuyUserAddressService {
    @Autowired
    private EasybuyUserAddressMapper easybuyUserAddressMapper;
    @Override
    public int xzAddress(EasybuyUserAddress easybuyUserAddress) {
        return easybuyUserAddressMapper.insert(easybuyUserAddress);
    }

    @Override
    public List<EasybuyUserAddress> myaddress(int userid) {
        return easybuyUserAddressMapper.myaddress(userid);
    }

    @Override
    public int insertAddress(EasybuyUserAddress easybuyUserAddress) {
        return easybuyUserAddressMapper.insert(easybuyUserAddress);
    }

    @Override
    public int deleteAdress(int id) {
        return easybuyUserAddressMapper.deleteById(id);
    }

    @Override
    public List<EasybuyUserAddress> addressbyisdefault(String isdefault,int userid) {
        return easybuyUserAddressMapper.addressbyisdefault(isdefault,userid);
    }

    @Override
    public int updatebyid(EasybuyUserAddress easybuyUserAddress){
        /*EasybuyUserAddress easybuyUserAddress = new EasybuyUserAddress();*/
        //easybuyUserAddress.setId(id);
        //easybuyUserAddress.setIsdefault(isdefault);
        return easybuyUserAddressMapper.updateById(easybuyUserAddress);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updatemoren(int id,List<EasybuyUserAddress> list,int userid) {
        try{
            for(EasybuyUserAddress easybuyUserAddress : list){
                easybuyUserAddress.setIsdefault("0");
                updatebyid(easybuyUserAddress);
            }
            EasybuyUserAddress easybuyUserAddress1 = new EasybuyUserAddress();
            easybuyUserAddress1.setIsdefault("1");
            easybuyUserAddress1.setId(id);
            easybuyUserAddress1.setUserid(userid);
            updatebyid(easybuyUserAddress1);
        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return 0;
        }
        return 1;
    }
}
